if
expressions: if
.
. . do
and if
. . .
then
. . . else.
if conditional do trueExpressionUse
if conditional then trueExpression else falseExpression
if
. . . do
if you are only
interested in the true
condition, and
if
. . . then
. . . else
if
both the true
and false
conditions
must be handled.
You can use if
. .
. then
without the else
statement,
but the compiler will be waiting for the else
statement, so you will not immediately see the result of an
if
. . . then
statement in the
Listener window. Once another expression has been entered,
however, you will see the results of both expressions. If you
type an if
. . . then
without the
else
and want to see the result right away, you
can use the operator !!
, which is legal only at
the top level when an expression is complete.
The conditional part of the if
expression is an expression that evaluates to either
false
or something other than false
.
In both forms of the if
expression,
trueExpression is evaluated if the value of
conditional is not false
. That is, the
test expression does not have to evaluate specifically to the
true
object for trueExpression to be
evaluated-it just has to evaluate to something other than
false
. Both trueExpression and
falseExpression are often compound expressions.
Both forms of the if
expression return a
value-that value is the value of either
trueExpression or falseExpression, depending
on which one was evaluated. Because if
expressions return a value, you can nest if
expressions inside other expressions, such as an
assignment.
If the test in the if
. . . do
expression returns false
, the whole
if
expression returns undefined
.
Here are some examples using if
expressions:
global x := 1, y := 5, p
if x > 0 do x := 0
p := if x > 0 then x else 0
if x < y then x
else (
print "x is greater than y, setting x to 0"
x := 0
)
case [ test ] ofThe value of test is compared to the value of each of the tags in the tagged expressions (the tag
tag:
expression
. . .
[ otherwise:
expression ]
end
:
expression clause) in
succession using the equal
global function as a
test of object equality. When this comparison first evaluates
to true
, then the expression (often a
compound expression) is evaluated. Once a match is found and
the expression is evaluated, the case
expression terminates and yields the value of that
expression. No other comparisons are made. Note that the
case
expression, like the if
expression, always yields a value. If there is no match, the
value is undefined
.
You can have any number of
tag:
expression clauses in a
case
expression. Whereas the expression
can be any expression (including a compound expression), the
tag is a limited form of expression, evaluated before
the return value of test is matched against it. The
tag can be one of the following:
45
,
"this or that"
, @nameMe
)
#(1, 2,
3)
)
myarray[1]
)
myrect.x1
)
nextMethod
(described in Chapter 6, "Defining Classes and
Objects."
otherwise
clause identifies the
default case, if none of the tags result in
true
:case pet of
@cat: print "meow"
@dog: print "woof"
@bird: print "chirp"
@snake: print "hiss"
otherwise: print "mysterious animal noise"
end
bool := case i of
0:false
1:true
otherwise: (print "out of range"; undefined)
end
If the test is omitted, the tags are expected to be
expressions that evaluate to true
or
false
. When the first tag expression to yield
true
is evaluated, the case
expression terminates and yields the value of its
corresponding expression, which it evaluates.
case of
(isComparable a b): (
case of
(a > b): print "a is greater than b"
(a = b): print "a is equal to b"
otherwise: print "a is less than b"
end
)
otherwise: print "a and b are not comparable"
end
If the otherwise
tag is omitted and none of the
other tags match the value of the test expression, the value
of the case
expression is
undefined
.
This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.